//登录js
//获取dom结构
let username = document.querySelector('#username');
let pwd = document.querySelector('#pwd');
let eye = document.querySelector('#eye');
let form = document.querySelector('form');
let btn = document.querySelector('.btn');
let reg = document.querySelector("#reg");
let rem = document.querySelector("#rem")
//console.log(rem.checked)
form.onsubmit=function(e){
    e = e || window
    e.preventDefault();
    //用户名和密码不能为空
    if(!username.value || !pwd.value){
        //alert('用户名和密码不能为空');
        var index = layer.msg(
            '用户名和密码不能为空哦',
            {icon: 2,shade:[0.8, '#393D49'],time:3000},
          );
        return;
    }
    //正则判断用户名格式
    let reg1 = /^[a-zA-Z]\w{4,15}$/
    if(!reg1.test(username.value)){
        //alert('用户名格式错误')
        layer.msg(
            '用户名格式错误',
            {icon: 5,shade:[0.8, '#393D49'],time:3000},
          );
        return;
    }
    //判断用户密码格式
    let reg2 = /^\w{6,16}$/
    if(!reg2.test(pwd.value)){
        //alert('密码格式错误')
        layer.msg(
            '密码格式错误',
            {icon: 5,shade:[0.8, '#393D49'],time:3000},
          );
        return;
    }
    //登录按钮颜色正常
    if(username.value !=''&&pwd.value !=''){
        btn.style.opacity = 1
    }
    //ajax请求
    ajax({
        url:'../php/login.php',
        data:{
            username : username.value,
            pwd:pwd.value,
        },
        type:'post',
        dataType:'json',
        fn:function(res){
            if(res.status == 1){//登录成功
                setCookie('username',username.value);
                if(!rem.checked){
                    //alert('请勾选小米用户协议')
                    layer.msg(
                        '请勾选小米用户协议',
                        {icon: 0,shade:[0.8, '#393D49'],time:3000},
                      );
                    return;
                }
                else{
                    //alert(res.msg)
                    layer.msg(
                        res.msg,
                        {icon: 1,time:500},
                      );
                    location.href = '../html/xiaomi.html'; 
                }
                
            }
           else{
                layer.msg(
                         res.msg,
                         {icon: 4,shade:[0.8, '#393D49'],time:3000},
                      );
           }
        }
    })
}

//密码眼睛睁开
       
eye.addEventListener('click',function(){
    if((eye.className == 'iconfont icon-biyanjing') && (pwd.type == 'password')){
        eye.className = 'iconfont icon-zhengkaiyanjing'
        pwd.type = 'text'
   
    }
    else{
        eye.className = 'iconfont icon-biyanjing'
        pwd.type = 'password'
    }})
//给注册加点击事件
reg.onclick = function(){
    reg.className='active'
    location.href='../html/register.html'
}

